{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4db470df-5710-44f2-9cf3-b132838b6b33",
   "metadata": {},
   "source": [
    "GPU计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "88112370-68cb-425a-b1cb-253cdc3032a4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sun Feb  2 18:16:10 2025       \n",
      "+-----------------------------------------------------------------------------------------+\n",
      "| NVIDIA-SMI 560.94                 Driver Version: 560.94         CUDA Version: 12.6     |\n",
      "|-----------------------------------------+------------------------+----------------------+\n",
      "| GPU  Name                  Driver-Model | Bus-Id          Disp.A | Volatile Uncorr. ECC |\n",
      "| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |\n",
      "|                                         |                        |               MIG M. |\n",
      "|=========================================+========================+======================|\n",
      "|   0  NVIDIA GeForce RTX 4070 ...  WDDM  |   00000000:01:00.0  On |                  N/A |\n",
      "| N/A   38C    P5              6W /  140W |    1106MiB /   8188MiB |      0%      Default |\n",
      "|                                         |                        |                  N/A |\n",
      "+-----------------------------------------+------------------------+----------------------+\n",
      "                                                                                         \n",
      "+-----------------------------------------------------------------------------------------+\n",
      "| Processes:                                                                              |\n",
      "|  GPU   GI   CI        PID   Type   Process name                              GPU Memory |\n",
      "|        ID   ID                                                               Usage      |\n",
      "|=========================================================================================|\n",
      "|    0   N/A  N/A      3396    C+G   ...1\\extracted\\runtime\\WeChatAppEx.exe      N/A      |\n",
      "|    0   N/A  N/A      4796    C+G   ...s\\System32\\ApplicationFrameHost.exe      N/A      |\n",
      "|    0   N/A  N/A      6084    C+G   ...x86)\\Lenovo\\SLBrowser\\SLBrowser.exe      N/A      |\n",
      "|    0   N/A  N/A      6324    C+G   ...CBS_cw5n1h2txyewy\\TextInputHost.exe      N/A      |\n",
      "|    0   N/A  N/A     10364    C+G   ...siveControlPanel\\SystemSettings.exe      N/A      |\n",
      "|    0   N/A  N/A     10772    C+G   ...ekyb3d8bbwe\\PhoneExperienceHost.exe      N/A      |\n",
      "|    0   N/A  N/A     12760    C+G   C:\\Windows\\explorer.exe                     N/A      |\n",
      "|    0   N/A  N/A     13576    C+G   C:\\Windows\\System32\\NahimicSvc64.exe        N/A      |\n",
      "|    0   N/A  N/A     16840    C+G   ...n\\131.0.2903.146\\msedgewebview2.exe      N/A      |\n",
      "|    0   N/A  N/A     16844    C+G   ...BrowserEngine\\BaiduNetdiskUnite.exe      N/A      |\n",
      "|    0   N/A  N/A     17192    C+G   ...2txyewy\\StartMenuExperienceHost.exe      N/A      |\n",
      "|    0   N/A  N/A     17404    C+G   ...nt.CBS_cw5n1h2txyewy\\SearchHost.exe      N/A      |\n",
      "|    0   N/A  N/A     17740    C+G   D:\\ToDesk\\ToDesk.exe                        N/A      |\n",
      "|    0   N/A  N/A     20324    C+G   ...les\\Microsoft OneDrive\\OneDrive.exe      N/A      |\n",
      "|    0   N/A  N/A     23748    C+G   ....5688.0_x64__8j3eq9eme6ctt\\IGCC.exe      N/A      |\n",
      "|    0   N/A  N/A     25460    C+G   ...__8wekyb3d8bbwe\\WindowsTerminal.exe      N/A      |\n",
      "|    0   N/A  N/A     26996    C+G   ...5n1h2txyewy\\ShellExperienceHost.exe      N/A      |\n",
      "|    0   N/A  N/A     28532    C+G   ...oogle\\Chrome\\Application\\chrome.exe      N/A      |\n",
      "|    0   N/A  N/A     32632    C+G   ...5n1h2txyewy\\AccountsControlHost.exe      N/A      |\n",
      "|    0   N/A  N/A     33388    C+G   ... Files\\Lenovo\\AIAgent\\XLSmartUI.exe      N/A      |\n",
      "|    0   N/A  N/A     36300    C+G   C:\\Windows\\System32\\ShellHost.exe           N/A      |\n",
      "|    0   N/A  N/A     45344    C+G   ...crosoft\\Edge\\Application\\msedge.exe      N/A      |\n",
      "|    0   N/A  N/A     47084      C   ...conda\\envs\\deep_learning\\python.exe      N/A      |\n",
      "|    0   N/A  N/A     58588    C+G   ...t.LockApp_cw5n1h2txyewy\\LockApp.exe      N/A      |\n",
      "+-----------------------------------------------------------------------------------------+\n"
     ]
    }
   ],
   "source": [
    "!nvidia-smi"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe28203c-dac3-4d12-89cb-1ec0251ddcad",
   "metadata": {},
   "source": [
    "查看GPU是否可用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f73d1a48-66a1-416d-b2cc-692cc9039655",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "torch.cuda.is_available() # 输出 True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a99534d7-4785-431a-8c3d-8feffaad3e41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看GPU数量\n",
    "torch.cuda.device_count() # 输出 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "40d12f2a-0393-4fec-80f0-f5a94c9d80f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看当前GPU索引号\n",
    "torch.cuda.current_device() # 输出 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6688e771-8836-4cfa-abd0-b3c7e0ebcd8d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'NVIDIA GeForce RTX 4070 Laptop GPU'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查看GPU名字\n",
    "torch.cuda.get_device_name(0) # 输出 'NVIDIA GeForce RTX 4070 Laptop GPU'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95d44a3a-0c6e-4557-98d6-2753bf6a9359",
   "metadata": {},
   "source": [
    "1.2GPU上TENSOR的计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "22381599-3b8a-4b85-a1ae-84f329a8e891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2, 3])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([1, 2, 3])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cac10746-4dea-4cc6-b5c2-c2a0cc9c454c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2, 3], device='cuda:0')"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = x.cuda(0)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7a356146-1e08-4eae-8c8d-7d9d4a6c6925",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "device(type='cuda', index=0)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dc500d00-07aa-4a80-b969-b187b8913685",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2, 3], device='cuda:0')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "x = torch.tensor([1, 2, 3], device=device)\n",
    "# or\n",
    "x = torch.tensor([1, 2, 3]).to(device)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "dff22eed-2fe0-44e9-a5ad-47582a256958",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 4, 9], device='cuda:0')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#如果在GPU上运算，结果也会存放在GPU上\n",
    "y = x**2\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d958c874-4772-446f-ba54-134c662d0ec5",
   "metadata": {},
   "source": [
    "存放在不同位置的数据不能直接进行计算，CPU上的数据不能与在GPU上的数据进行运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "39233f99-b346-47a0-9bbc-7552c45d67b2",
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[11], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m z \u001b[38;5;241m=\u001b[39m \u001b[43my\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcpu\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!"
     ]
    }
   ],
   "source": [
    "z = y + x.cpu()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0aedc27-e723-4d3c-8256-4aa7eed5706f",
   "metadata": {},
   "source": [
    "1.3模型的GPU计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "615041fb-ac35-4996-88b4-7f650118e375",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "device(type='cpu')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#检查模型的参数device的属性\n",
    "net = nn.Linear(3, 1)\n",
    "list(net.parameters())[0].device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e386aa39-1048-4ecd-a97d-99751c1c5f08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "device(type='cuda', index=0)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将CPU上的模型转换到GPU上计算\n",
    "net.cuda()\n",
    "list(net.parameters())[0].device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1d09488f-3823-48ad-99f3-8e09ffcb407a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.3303],\n",
       "        [-0.2321]], device='cuda:0', grad_fn=<AddmmBackward0>)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.rand(2,3).cuda()\n",
    "net(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43f7c5de-37ec-4e59-ba01-726d3efb16e0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:pytorch]",
   "language": "python",
   "name": "deep_learning"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
